#include "head.h"

int main_tcp(int* fd, const char* ip, const char* port)
{
    *fd = socket(AF_INET, SOCK_STREAM, 0);
    if(*fd == -1)
        error(1, errno, "socket");

    // 防止端口占用
    int resue = 1;
    setsockopt(*fd, SOL_SOCKET, SO_REUSEADDR, &resue, sizeof(resue));

    struct sockaddr_in addr;
    addr.sin_family = AF_INET;
    addr.sin_port = htons(atoi(port));
    addr.sin_addr.s_addr = inet_addr(ip);
    if(bind(*fd, (struct sockaddr*)&addr, sizeof(addr)) == -1)
        error(1, errno, "bind");

    if(listen(*fd, N) == -1)
        error(1, errno, "listen");

    return 0;
}
